﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework;
using System.Windows.Threading;

namespace girapong
{
    public class SolidBlock: solid
    {

        public System.Windows.Shapes.Rectangle shape { get; set; }
        public DispatcherTimer timeToAffectSpeed { get; set; } // Tiempo para cambiar dirección del movimiento

        public SolidBlock(rally rally, double x, double y, double w, double h)
        {
            shape = new System.Windows.Shapes.Rectangle();
            Random n = new Random();
            shape.Fill = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, (byte)(55 + n.Next(0, 200)), (byte)(55 + n.Next(0, 200)), (byte)(55 + n.Next(0, 200))));
            setPosition(new System.Windows.Point(x, y));
            shape.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            shape.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            shape.Width = w;
            shape.Height = h;
            dtSpeed = new DispatcherTimer();
            timeToAffectSpeed = new DispatcherTimer();
            timeToAffectSpeed.Interval = TimeSpan.FromMilliseconds(5000); 
            timeToAffectSpeed.Tick += new EventHandler(this.refreshSpeed);
            resetSpeed(general.generateRandomVectorByModule(1));
            dtSpeed.Start();
            rally.LayoutRoot.Children.Add(shape);
            rallyReference = rally;
        }

        public void setPosition(System.Windows.Point p)
        {
            position = p;
            shape.Margin = new Thickness(position.X, position.Y, 0, 0);
        }
        public void resetSpeed(Vector2 speed)
        {
            dtSpeed.Interval = TimeSpan.FromMilliseconds(10); 
            dtSpeed.Tick += new EventHandler(this.refreshBlock);
            modifySpeed(speed);
        }
        public void modifySpeed(Vector2 velocidad)
        {
            this.speed = velocidad;
        }
        public void refreshBlock(object sender, EventArgs e)
        {
            Vector2 vUnitary = new Vector2(speed.X, speed.Y);

            if (speed.X != 0) position.X += vUnitary.X;
            if (speed.Y != 0) position.Y += vUnitary.Y;
            shape.Margin = new Thickness(position.X, position.Y, 0, 0);

            if ((position.X <= 0) || (position.X >= screen.WIDTH-shape.Width))
            {
                modifySpeed(new Vector2(-speed.X, speed.Y));
            }
            if ((position.Y <= 0) || (position.Y >= screen.HEIGHT - 100 - shape.Height - rallyReference.barraDcha.Width))
            {
                modifySpeed(new Vector2(speed.X, -speed.Y));
            }
        }
        public void refreshSpeed(object sender, EventArgs e)
        {
            resetSpeed(general.generateRandomVectorByModule(1));
        }
    }
}
